bodyApp.controller('personDcController', function ($scope, $http, phpData) {

    let api_url = phpData.classLink;

    $scope.loading = true;

    $scope.query = {
        'stext': '',
        'author_id':phpData.author_id,
        'calculate_number': true
    };

    $scope.paging = {
        'cur_page': 1,
        'per_page': 1,
        'all_page': 1,
        'total': 1,
        'offset': 0
    };


    $scope.data = [];


    let fnc_calPageOffset = function (page) {

        let offset = (page - 1) * $scope.paging.per_page;

        if (offset >= 0) {
            $scope.paging.cur_page = page;
            return offset
        } else {
            $scope.paging.cur_page = 1;
            return 0;
        }

    };

    let fnc_numPage = function (total, per_page) {
        return per_page > 0 ? Math.ceil(total / per_page) : 0;
    };

    $scope.changePage = function (page) {
        page = parseInt(page);
        if (page < 1) page = 1;
        if (page > $scope.paging.all_page) page = $scope.paging.all_page;
        $scope.paging.offset = fnc_calPageOffset(page);
        fnc_search();
    };

    $scope.fillPage = function () {
        $scope.changePage($scope.paging.cur_page);
    };

    let fnc_search = function () {
        $scope.loading = true;

        /*document.body.scrollTop = 0;
        document.documentElement.scrollTop = 0;*/

        $scope.data = [];
        $http.post(api_url + 'findCreatorItem/' + $scope.paging.offset,
            'data=' + encodeURIComponent(angular.toJson($scope.query)),
            {headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'}}
        ).then(function (res) {

            $scope.data = res.data.result;

            if (typeof(res.data.metadata) !== 'undefined') {
                $scope.paging.per_page = res.data.metadata.per_page;
                $scope.paging.total = res.data.metadata.total;
                $scope.paging.all_page = fnc_numPage($scope.paging.total, $scope.paging.per_page);
            }

            $scope.query.calculate_number = false;
            $scope.loading = false;
        });

    };


    $scope.actionReset = function () {
        $scope.query.stext = '';
        $scope.query.calculate_number = true;
        $scope.changePage(1);
    };

    $scope.actionSearch = function () {
        $scope.query.calculate_number = true;
        $scope.changePage(1);
    };

    let fnc_init = function () {
        fnc_search();
    };

    fnc_init();

});
